import * as energyService from '@/services/energy';

export default {
  namespace: 'energyStatistics',
  state: {
    h: '',
    p: 0,
    t: 0,
    names: [],
    lists: [],
    startTime: '',
    endTime: '',
    tDatas: [],
    price: '[{"s":0,"e":-1,"c":1,"p":1}]'
  },
  reducers: {
    setData(state, { payload }) {
      return { ...state, ...payload };
    },
  },
  effects: {
    *fetchUnits({ payload: { h, p, t, period, startTime, endTime, price } }, { call, put, select }) {
      h = (h!==undefined)?h:yield select(state => state.energyStatistics.h);
      t = (t!==undefined)?t:yield select(state => state.energyStatistics.t);
      p = (p!==undefined)?p:yield select(state => state.energyStatistics.p);
      startTime = (startTime!==undefined)?startTime:yield select(state => state.energyStatistics.startTime);
      endTime = (endTime!==undefined)?endTime:yield select(state => state.energyStatistics.endTime);
      price = (price!==undefined)?price:yield select(state => state.energyStatistics.price);
      const response = yield call(energyService.fetchUnits, { h, p, t, startTime, endTime, price});
      if (response && typeof response.state !== undefined && response.state === 0) {
        let names=[], lists=[], tDatas=[];
        response.data.datas.forEach((data)=>{
          names.push(data.name);
          lists.push(data.list);
        });
        if(response.data.tDatas) {
          tDatas = response.data.tDatas;
        }
        yield put({ type: 'setData', payload: { h, p, t, names, lists, startTime, endTime, tDatas, price } });
      } else {
        yield put({
          type: 'setData',
          payload: { h:'', p:0, t:0, names:[], lists:[], startTime:'', endTime:'', tDatas:[], price:'[]'},
        });
      }
    },
    *fetchSum({ payload: { h, p, period, startTime, endTime, price } }, { call, put, select }) {
      h = (h!==undefined)?h:yield select(state => state.energyStatistics.h);
      p = (p!==undefined)?p:yield select(state => state.energyStatistics.p);
      startTime = (startTime!==undefined)?startTime:yield select(state => state.energyStatistics.startTime);
      endTime = (endTime!==undefined)?endTime:yield select(state => state.energyStatistics.endTime);
      price = (price!==undefined)?price:yield select(state => state.energyStatistics.price);
      const response = yield call(energyService.fetchSum, { h, p, startTime, endTime, price});
      if (response && typeof response.state !== undefined && response.state === 0) {
        let lists=[];
        response.data.datas.forEach((data)=>{
          lists.push(data.list);
        });
        yield put({ type: 'setData', payload: { h, p, lists, startTime, endTime, price } });
      } else {
        yield put({
          type: 'setData',
          payload: { h:'', p:0, lists:[], startTime:'', endTime:'', price:'[]'},
        });
      }
    },
    *fetchSame({ payload: { h, p, period, startTime, endTime, price } }, { call, put, select }) {
      h = (h!==undefined)?h:yield select(state => state.energyStatistics.h);
      p = (p!==undefined)?p:yield select(state => state.energyStatistics.p);
      startTime = (startTime!==undefined)?startTime:yield select(state => state.energyStatistics.startTime);
      endTime = (endTime!==undefined)?endTime:yield select(state => state.energyStatistics.endTime);
      price = (price!==undefined)?price:yield select(state => state.energyStatistics.price);
      const response = yield call(energyService.fetchSame, { h, p, startTime, endTime, price});
      if (response && typeof response.state !== undefined && response.state === 0) {
        let lists=[];
        response.data.datas.forEach((data)=>{
          lists.push(data.list);
        });
        yield put({ type: 'setData', payload: { h, p, lists, startTime, endTime, price } });
      } else {
        yield put({
          type: 'setData',
          payload: { h:'', p:0, lists:[], startTime:'', endTime:'', price:'[]'},
        });
      }
    },
    *fetchProportionTrend({ payload: { h, p, t, period, startTime, endTime, price } }, { call, put, select }) {
      h = (h!==undefined)?h:yield select(state => state.energyStatistics.h);
      t = (t!==undefined)?t:yield select(state => state.energyStatistics.t);
      p = (p!==undefined)?p:yield select(state => state.energyStatistics.p);
      startTime = (startTime!==undefined)?startTime:yield select(state => state.energyStatistics.startTime);
      endTime = (endTime!==undefined)?endTime:yield select(state => state.energyStatistics.endTime);
      price = (price!==undefined)?price:yield select(state => state.energyStatistics.price);
      const response = yield call(energyService.fetchProportionTrend, { h, p, t, startTime, endTime, price});
      if (response && typeof response.state !== undefined && response.state === 0) {
        let names=[], lists=[], tDatas=[];
        response.data.datas.forEach((data)=>{
          names.push(data.name);
          lists.push(data.list);
        });
        if(response.data.tDatas) {
          tDatas = response.data.tDatas;
        }
        yield put({ type: 'setData', payload: { h, p, t, names, lists, startTime, endTime, tDatas, price } });
      } else {
        yield put({
          type: 'setData',
          payload: { h:'', p:0, t:0, names:[], lists:[], startTime:'', endTime:'', tDatas:[], price:'[]'},
        });
      }
    },
    *setHandle({ payload: {h} }, { put }) {
      yield put({ type: 'setData', payload: {h}});
    },
    *clearAll({ _ }, { put }) {
      yield put({ type: 'setData', payload: { h:'', p:0, t:0, names:[], lists:[], startTime:'', endTime:'', tDatas:[], price:'[]'}});
    }
  }
}
